/**
 * OpenAIWhisper.js - OpenAI Whisper 音频转录 API 调用模块
 * 这个模块提供了与OpenAI Whisper API交互的功能，支持音频转录
 * @module OpenAIWhisper
 */

// 导入必要的依赖模块
const fs = require('fs');
const OpenAI = require('openai');
const propertyChecker = require('../plugins/inTypeOf');  // 用于检查对象属性

// API配置（根据用户示例调整）
const openai = new OpenAI({
    apiKey: "sk-by9p3LlxVz5fu2TPLzi2GcxQIyVxQEmHLEZd6xj8H3CIEQxt",
  // apiKey: 'sk-43DJyf43ZNe9e9V1klQ9whm0Z1OvGqWhcEM7zUI6hqVQh904',  // 需用户替换为实际API Key  sk-by9p3LlxVz5fu2TPLzi2GcxQIyVxQEmHLEZd6xj8H3CIEQxt
  baseURL: 'https://jeniya.cn/v1'  // 自定义API基础URL
});

/**
 * 使用OpenAI Whisper API进行音频转录的异步函数
 * @param {string} filePath - 输入的音频文件路径
 * @returns {Promise<Object>} - 返回包含转录结果或错误信息的对象
 *                             成功时返回 {text: string, apiError: 'normal!', reqStatus: 'success'}
 *                             失败时返回 {reqStatus: string, error: string}
 */
 

async function generateContent(prompt) {
    if (!prompt || typeof prompt !== 'string') {
        return {
            reqStatus: 'error',
            error: 'Invalid input: prompt must be a non-empty string'
        };
    }

    try {
        const completion = await openai.chat.completions.create({
            messages: [{ role: 'user', content: prompt }],
          // model: 'gemini-2.0-flash',
          model:'gemini-2.0-flash-lite'
        });

        return {
            id: completion.id,
            object: 'chat.completion',
            // created: Math.floor(Date.now() / 1000),
          // model: 'gemini-2.0-flash',
          // model: 'qwen3-30b-a3b-think',
            
            usage: {
                prompt_tokens: completion.usage.prompt_tokens,
                completion_tokens: completion.usage.completion_tokens,
                total_tokens: completion.usage.total_tokens
            },
            choices: [{
                message: {
                    // role: 'assistant',
                    content: completion.choices
              },
             
                // finish_reason: 'stop',
                // index: 0
          }],
          parts: completion.choices,
            reqStatus: 'success'
        };

    } catch (error) {
        return {
            reqStatus: 'error',
            error: error.message,
            apiError: error.type || 'API请求错误'
        };
    }
}

// 导出transcribeAudio函数供其他模块调用
module.exports = { generateContent };